package grafico;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import voxToolkit.FabricaVoxToolkit;
import voxToolkit.VoxButton;
import voxToolkit.VoxCombo;
import voxToolkit.VoxTextField;

import main.Contexto;

public class WndPrincipal extends JFrame
{
	static final long serialVersionUID = 1;

	private JPanel painel_superior;
	private JPanel painel_inferior;
	private JEditorPane painel_texto;

	private JPanel controle_esquerda;
	private JPanel controle_centro;
	private JPanel controle_direita;

	private JPanel painel_navegacao;
	private JPanel painel_velo_nivel;
	private JPanel painel_botoes;
	private JPanel painel_pagina;

	private VoxButton btnNavPlay;
	private VoxButton btnNavStop;
	private VoxButton btnNavPrevious;
	private VoxButton btnNavVolta;
	private VoxButton btnNavAvanca;
	private VoxButton btnNavProximo;
	private VoxButton btnSemSom;
	private VoxButton btnComSom;
	private VoxButton btnBtnListar;
	private VoxButton btnBtnAbrir;
	private VoxButton btnBtnBusca;
	private VoxButton btnBtnHelp;
	private VoxButton btnBtnInfo;
	private VoxButton btnBtnConfig;

	private JLabel lbMecDaisy;
	private JButton logoDaisy;
	private JButton logoNCE;
	private JButton logoMEC;

	private JPanel playerMecDaisy;
	private JPanel pnLogos;
	private JPanel painel_nivel;

	private JLabel lbNivel;
	private VoxCombo cmbNivel;
	private JLabel lbPagina;
	private VoxTextField txPagina;
	
	private GerenteGUI gerente;
	
	private ControleEventos ctrlEventos;
	
	private Contexto contexto;
	

	
	public WndPrincipal( GerenteGUI pai )
	{
		super();
		
		gerente = pai;
		contexto = Contexto.instancia();
		ctrlEventos = ControleEventos.instancia();
		
		initGUI();
		
		addWindowListener(ctrlEventos);
	}

	public JEditorPane getTxtPane() 
	{
		return painel_texto;
	}
	
	public void setLayoutModoContinuo()
	{
		if ( btnNavPlay.isVisible() )
		{
			btnNavPlay.setVisible(false);
			btnNavStop.setVisible(true);
		}
	}
	
	public void setLayoutModoParagrafo()
	{
		if ( btnNavPlay.isVisible() )
		{
			btnNavPlay.setVisible(false);
			btnNavStop.setVisible(true);
		}
	}
	
	public void setLayoutModoParado()
	{
		if ( btnNavStop.isVisible() )
		{
			btnNavPlay.setVisible(true);
			btnNavStop.setVisible(false);
		}
	}
	
	public void ativaBotaoStop()
	{
		btnNavStop.setFocusMudo();
	}
	
	private void initGUI() 
	{
		try 
		{
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			
			setTitle("MECDaisy - v1.0");

			/*
			 *  Captura o evento de teclar espa�o no JFrame
			 */
			getRootPane().getActionMap().put("SpaceEvent", ctrlEventos.getSpaceAction());
			InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
			im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "SpaceEvent"); 

			Container principal = getContentPane();
			
			principal.setLayout(new BorderLayout());
			principal.setBackground(Fabrica.background);
			principal.setPreferredSize(new Dimension(800,600));
			principal.setMaximumSize(new Dimension(800,600));
			principal.setMinimumSize(new Dimension(800,600));

			/*
			 * Painel superior
			 */
			painel_superior = Fabrica.painelFundoAzul();
			painel_superior.setPreferredSize(new java.awt.Dimension(800, 60));
			painel_superior.setLayout(new BoxLayout(painel_superior, BoxLayout.X_AXIS));
			
			principal.add(painel_superior, BorderLayout.NORTH);

			/*
			 * Area de exibicao do texto html
			 */
			painel_texto = new JEditorPane();
			painel_texto.setEditable(false);
			painel_texto.setContentType("text/html");
			painel_texto.setBackground( gerente.toColor( contexto.getCorFundo() ) );
			painel_texto.setFocusable(false);
			
			principal.add(painel_texto, BorderLayout.CENTER);

			/*
			 * Painel inferior
			 */
			painel_inferior = FabricaVoxToolkit.painelFundoAzul();
			painel_inferior.setLayout(new BorderLayout());
			painel_inferior.setPreferredSize(new Dimension(800, 50));
			
			playerMecDaisy = FabricaVoxToolkit.painelFundoAzul();
			playerMecDaisy.setPreferredSize(new Dimension(450, 50));
			
			lbMecDaisy = FabricaVoxToolkit.labelBranco("MECDaisy");
			lbMecDaisy.setFont(FabricaVoxToolkit.fonteGrande);
			lbMecDaisy.setFocusable(false);
			playerMecDaisy.add(lbMecDaisy);
			
			painel_inferior.add(playerMecDaisy,BorderLayout.WEST);
			
			pnLogos = FabricaVoxToolkit.painelFundoAzul();
			pnLogos.setLayout(new BorderLayout());
			pnLogos.setPreferredSize(new Dimension(300,50));
			pnLogos.setMaximumSize(new Dimension(300,50));
			pnLogos.setMinimumSize(new Dimension(300,50));
			painel_inferior.add(pnLogos,BorderLayout.EAST);
			
			logoDaisy = Fabrica.botaoLogo("daisy");
			logoDaisy.setFocusable(false);
			logoNCE = Fabrica.botaoLogo("nce");
			logoNCE.setFocusable(false);
			logoMEC = Fabrica.botaoLogo("mec");
			logoMEC.setFocusable(false);
			
			pnLogos.add(logoDaisy,BorderLayout.WEST);
			pnLogos.add(logoMEC,BorderLayout.CENTER);
			pnLogos.add(logoNCE,BorderLayout.EAST);

			principal.add(painel_inferior, BorderLayout.SOUTH);

			/*
			 * Controle esquerda
			 */
			controle_esquerda = FabricaVoxToolkit.painelFundoAzul();
			painel_superior.add(Box.createRigidArea(new Dimension(10, 90)));
			painel_superior.add(controle_esquerda, 1);
			
			controle_esquerda.setLayout(new BoxLayout(controle_esquerda, BoxLayout.Y_AXIS));
			
			painel_navegacao = FabricaVoxToolkit.painelFundoAzul();
			painel_navegacao.setLayout(new BoxLayout(painel_navegacao, BoxLayout.X_AXIS));
			controle_esquerda.add(painel_navegacao);

			painel_velo_nivel = FabricaVoxToolkit.painelFundoAzul();
			painel_velo_nivel.setLayout(new BoxLayout(painel_velo_nivel, BoxLayout.X_AXIS));
			controle_esquerda.add(painel_velo_nivel);

			btnNavPrevious = new VoxButton("nivel_anterior", "Nível Anterior");
			btnNavPrevious.addActionListener(ctrlEventos);
			btnNavPrevious.addKeyListener(ctrlEventos);
			painel_navegacao.add(btnNavPrevious);

			btnNavVolta = new VoxButton("retrocede", "Retroceder");
			btnNavVolta.addActionListener(ctrlEventos);
			btnNavVolta.addKeyListener(ctrlEventos);
			painel_navegacao.add(btnNavVolta);

			/*
			 * Botões que se alternam Play/Stop
			 */
			btnNavPlay = new VoxButton("play",	"Tocar");
			btnNavPlay.addActionListener(ctrlEventos);
			btnNavPlay.addKeyListener(ctrlEventos);
			
			btnNavStop = new VoxButton("stop", "Parar");
			btnNavStop.addActionListener(ctrlEventos);
			btnNavStop.addKeyListener(ctrlEventos);
			
			btnNavPlay.setReverso(btnNavStop);
			btnNavStop.setReverso(btnNavPlay);
			btnNavStop.setVisible(false);

			painel_navegacao.add(btnNavPlay);
			painel_navegacao.add(btnNavStop);

			btnNavAvanca = new VoxButton("avanca", "Avançar");
			btnNavAvanca.addActionListener(ctrlEventos);
			btnNavAvanca.addKeyListener(ctrlEventos);
			painel_navegacao.add(btnNavAvanca);

			btnNavProximo = new VoxButton("proximo_nivel", "Próximo Nível");
			btnNavProximo.addActionListener(ctrlEventos);
			btnNavProximo.addKeyListener(ctrlEventos);
			painel_navegacao.add(btnNavProximo);

			/*
			 * Botões que se alternam ComSom/SemSom
			 */
			btnSemSom = new VoxButton("sem_som", "Tirar o Som");
			btnSemSom.addActionListener(ctrlEventos);
			btnSemSom.addKeyListener(ctrlEventos);
			btnSemSom.setAlignmentX(CENTER_ALIGNMENT);

			btnComSom = new VoxButton("com_som", "Tocar Som");
			btnComSom.addActionListener(ctrlEventos);
			btnComSom.addKeyListener(ctrlEventos);
			btnComSom.setAlignmentX(CENTER_ALIGNMENT);

			btnSemSom.setReverso(btnComSom);
			btnComSom.setReverso(btnSemSom);
			btnComSom.setVisible(false);

			painel_navegacao.add(btnSemSom);
			painel_navegacao.add(btnComSom);


			/*
			 * Controle centro
			 */
			controle_centro = FabricaVoxToolkit.painelFundoAzul();
			controle_centro.setPreferredSize(new Dimension(250, 90));
			controle_centro.setMinimumSize(new Dimension(250, 90));
			controle_centro.setMaximumSize(new Dimension(250, 90));
			controle_centro.setLayout(new BoxLayout(controle_centro, BoxLayout.X_AXIS));

			painel_pagina = FabricaVoxToolkit.painelFundoAzul();
			painel_pagina.setLayout(new BoxLayout(painel_pagina, BoxLayout.X_AXIS));
			
			lbPagina = FabricaVoxToolkit.labelBranco("Pág: ");
			lbPagina.setFocusable(false);
			painel_pagina.add(lbPagina);

			txPagina = new VoxTextField("Página");
			Dimension dimTxPag = new Dimension(70, 30);
			txPagina.setPreferredSize(dimTxPag);
			txPagina.setMinimumSize(dimTxPag);
			txPagina.setMaximumSize(dimTxPag);
			txPagina.setFocusable(true);
			painel_pagina.add(txPagina);

			//controle_centro.add(Box.createRigidArea(new Dimension(10, 10)));
			controle_centro.add(painel_pagina);

			painel_nivel = FabricaVoxToolkit.painelFundoAzul();
			painel_nivel.setLayout(new BoxLayout(painel_nivel, BoxLayout.X_AXIS));
			
			lbNivel = FabricaVoxToolkit.labelBranco("Nível: ");
			lbNivel.setFocusable(false);
			painel_nivel.add(lbNivel);
			
			String[] niveis = { "1", "2", "3", "4", "5", "6" };
			cmbNivel = new VoxCombo(niveis, "cmbNivel","Nível");
			Dimension dimCombo = new Dimension(50, 30);
			cmbNivel.setSize(dimCombo);
			cmbNivel.setMinimumSize(dimCombo);
			cmbNivel.setMaximumSize(dimCombo);
			cmbNivel.setPreferredSize(dimCombo);
			cmbNivel.setFocusable(true);

			painel_nivel.add(cmbNivel);
			controle_centro.add(Box.createRigidArea(new Dimension(10, 10)));
			controle_centro.add(painel_nivel);
			controle_centro.add(Box.createRigidArea(new Dimension(10, 10)));

			painel_superior.add(Box.createRigidArea(new Dimension(10, 90)));
			painel_superior.add(controle_centro);
			painel_superior.add(Box.createRigidArea(new Dimension(5, 90)));

			/*
			 * Controle direita
			 */
			controle_direita = FabricaVoxToolkit.painelFundoAzul();
			painel_superior.add(controle_direita);
			painel_superior.add(Box.createRigidArea(new Dimension(5, 90)));
			controle_direita.setLayout(new BoxLayout(controle_direita, BoxLayout.Y_AXIS));
			painel_botoes = FabricaVoxToolkit.painelFundoAzul();
			painel_botoes.setLayout(new BoxLayout(painel_botoes, BoxLayout.X_AXIS));

			controle_direita.add(painel_botoes);

			btnBtnAbrir = new VoxButton("abrir", "Abrir");
			btnBtnAbrir.addActionListener(ctrlEventos);
			btnBtnAbrir.addKeyListener(ctrlEventos);
			painel_botoes.add(btnBtnAbrir);

			btnBtnListar = new VoxButton("listar", "Mostrar Estrutura");
			btnBtnListar.addActionListener(ctrlEventos);
			btnBtnListar.addKeyListener(ctrlEventos);
			painel_botoes.add(btnBtnListar);

			/*
			 * Botões que se alternam ComTexto/SemTexto
			 */
			btnBtnBusca = new VoxButton("busca", "Buscar");
			btnBtnBusca.addActionListener(ctrlEventos);
			btnBtnBusca.addKeyListener(ctrlEventos);
			painel_botoes.add(btnBtnBusca);

			btnBtnConfig = new VoxButton("config", "Configurações");
			btnBtnConfig.addActionListener(ctrlEventos);
			btnBtnConfig.addKeyListener(ctrlEventos);
			painel_botoes.add(btnBtnConfig);

			btnBtnInfo = new VoxButton("info", "Informações sobre o texto");
			btnBtnInfo.addActionListener(ctrlEventos);
			btnBtnInfo.addKeyListener(ctrlEventos);
			painel_botoes.add(btnBtnInfo);

			btnBtnHelp = new VoxButton("help", "Ajuda");
			btnBtnHelp.addActionListener(ctrlEventos);
			btnBtnHelp.addKeyListener(ctrlEventos);
			painel_botoes.add(btnBtnHelp);

			this.setSize(800, 600);

		} catch (Exception e) 
		{
			e.printStackTrace();
		}
	}	
}
